import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-19
 * Time: 21:40
 */
public class test2 {
    /*
    LeetCode 560  和为K的子数组
     */

    public int subarraySum(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,1);
        int ret =0;
        int sum=0;
        for(int num:nums){
            sum += num;   // 计算前缀和
            ret += hash.getOrDefault(sum-k,0);   // 统计结果
            hash.put(sum,hash.getOrDefault(sum,0)+1);   // 将当前前缀和放入哈希表中
        }
        return ret;
    }
}
